/**
 * 
 */
package com.gmail.blackdog1987.carcare.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.activity.PromotionJoinActivity;
import com.gmail.blackdog1987.carcare.adapter.PromotionAdapter;
import com.gmail.blackdog1987.carcare.entity.TPromotion;
import com.gmail.blackdog1987.carcare.entity.base.DataPage;
import com.gmail.blackdog1987.carcare.network.request.builder.PromotionRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.network.response.PromotionResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.PromotionResponseHandler;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;

/**
 * @author wdy
 *
 */
public class PromotionFragment extends Fragment implements OnClickListener, OnResponseListener, OnItemClickListener, OnLastItemVisibleListener,
		OnRefreshListener2<ListView> {
	private View mContentView;

	PromotionRequestBuilder mReq = new PromotionRequestBuilder();
	PromotionResponseHandler mHandler = new PromotionResponseHandler();

	PullToRefreshListView list;
	PromotionAdapter adapter;

	List<TPromotion> promotions;
	
	DataPage<TPromotion> page;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.fragment_promotion, null);
		reg();
		initData();
		return mContentView;
	};

	public void initData() {
		list = (PullToRefreshListView) mContentView.findViewById(R.id.promotion_list);
		list.setMode(Mode.BOTH);
		list.setOnRefreshListener(this);
		list.setOnLastItemVisibleListener(this);
		adapter = new PromotionAdapter(getActivity());
		list.setAdapter(adapter);
		mHandler.setOnResponseListener(this);
		mReq.setPageIndex(1).setPageSize(20).postJsonRequest(getActivity(), mHandler);
		list.setOnItemClickListener(this);
	}

	private void reg() {

	}

	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("MainScreen"); // 统计页面
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("MainScreen");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		IResponse res = classz.getResponse();
		if (res instanceof PromotionResponse) {
			page = ((PromotionResponse)classz.getResponse()).datas;
			if(page.currentPageNo <= 1){
				adapter.setDatas(page.datas);
			}else{
				adapter.addDatas(page.datas);
				adapter.notifyDataSetChanged();
			}
			list.onRefreshComplete();
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_LONG).show();
		list.onRefreshComplete();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent i = new Intent(getActivity(), PromotionJoinActivity.class);
		i.putExtra("data", adapter.getItem(position-1));
		startActivity(i);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		mReq.setPageIndex(1).setPageSize(20).postJsonRequest(getActivity(), mHandler);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		if (page == null) {
			mReq.setPageIndex(1).setPageSize(20).postJsonRequest(getActivity(), mHandler);
		} else if (page.currentPageNo >= page.lastPageNo) {
			Toast.makeText(getActivity(), "没有了", Toast.LENGTH_LONG).show();
			list.onRefreshComplete();
		} else {
			mReq.setPageIndex(page.nextPage).setPageSize(page.pageSize).postJsonRequest(getActivity(), mHandler);
		}
	}

	@Override
	public void onLastItemVisible() {
	}

}
